/*
 * Licensed to Nanjing RedOrange Co. ltd (RedOrange) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * RedOrange licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package redora.rdo.util;

import java.io.PrintWriter;
import java.util.Set;

import redora.rdo.api.BusinessRuleViolation;

/**
 * Translate a (set of) business rule violations to JSON.
 * //TODO do i18n
 * Translates the error messages to the users language.
 * @author jaap
 *
 */
public class BusinessRuleJSON {
	/** Copies the BusinessRuleViolation to JSON. */
	public static void copy(BusinessRuleViolation in, PrintWriter out) {
		//TODO are all fields mandatory?
		out.print('{');
    	out.print("\"persistenceClass\":");
      	out.print(in.getClass().getName());
		out.print(',');
		out.print("\"id\":");
		out.print(in.getId());
	    out.print(',');
	    out.print("\"field\":");
	    out.print(in.getField().name());
	    out.print(',');
    	out.print("\"businessRuleId\":");
    	out.print(in.getBusinessRuleId());
	    out.print(',');
    	out.print("\"action\":");
    	out.print(in.getAction().name());
    	//TODO also do messageArguments, don't forget to escape free text!
		out.print('}');
	}
	
	/** Copies the BusinessRuleViolation(s) to JSON. */
	public static void copy(Set<BusinessRuleViolation> in, PrintWriter out) {
		out.print('[');
		char comma = ' ';
		for (BusinessRuleViolation br : in) {
			out.print(comma);
			copy(br, out);
			comma = ',';
		}
		out.print(']');
	}
}
